package cn.tedu.csmall.product.repo.impl;

import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Repository
public class BrandRedisRepositoryImpl implements IBrandRedisRepository {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void save(BrandStandardVO brandStandardVO) {
        String key = BRAND_ITEM_KEY_PREFIX + brandStandardVO.getId();
        ValueOperations<String, Serializable> opsForValue = redisTemplate.opsForValue();
        opsForValue.set(key, brandStandardVO);
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(BRAND_ALL_KEYS_KEY,key);
    }

    @Override
    public void saveList(List<BrandListItemVO> brandListItemVOS) {
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (BrandListItemVO item : brandListItemVOS) {
            ops.rightPush(BRAND_LIST_KEY,item);
        }
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(BRAND_ALL_KEYS_KEY,BRAND_LIST_KEY);
    }

    @Override
    public void deleteAll() {
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        Set<Serializable> serializableSet = opsForSet.members(BRAND_ALL_KEYS_KEY);
        Set<String> keys = new HashSet<>();
        for (Serializable serializable : serializableSet) {
            keys.add((String) serializable);
        }
        redisTemplate.delete(keys);
        redisTemplate.delete(BRAND_ALL_KEYS_KEY);
    }

    @Override
    public BrandStandardVO get(Long id) {
        String key = BRAND_ITEM_KEY_PREFIX + id;
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get(key);
        if (serializable != null) {
            return (BrandStandardVO) serializable;
        }
        return null;
    }

    @Override
    public List<BrandListItemVO> getList() {
        return getList(0,-1);
    }

    @Override
    public List<BrandListItemVO> getList(long start,long end) {
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(BRAND_LIST_KEY, start, end);
        List<BrandListItemVO> brandListItemVOList = new ArrayList<>();
        for (Serializable serializable: list) {
            brandListItemVOList.add((BrandListItemVO) serializable);
        }
        return brandListItemVOList;
    }
}
